<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-button type="primary" icon="el-icon-plus"  @click="showEdit=true;">添加</el-button>
       
      <el-table class="mt-20" :data="list">
        <el-table-column prop="id" label="序号" />
        <el-table-column prop="name" label="增值服务名称" />
        <el-table-column prop="price" label="增值服务价格" />
        <el-table-column prop="costPrice" label="增值服务成本价格" />
        <el-table-column prop="text" label="操作" fixed="right" >
          <template slot-scope="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.row,scope.$index)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
       </el-tabs>
      </el-card> 
    </div>
    <!-- 分页 -->
    <div align="center">
      <!-- <el-pagination
        :current-page.sync="currentPage"
        layout="prev, pager, next, jumper"
        :total="total"
      /> -->
    </div>
    <!-- 编辑/新建弹窗 -->
    <el-dialog :visible="showEdit" @close="showEdit=false;formData={}" label-width="400px">
      <el-form label-width="260px">
        <el-form-item label="id" prop="id" v-if="formData.id">
          <el-input v-model="formData.id" type="number" disabled />
        </el-form-item>
        <el-form-item label="增值服务名称" prop="name">
          <el-input v-model="formData.name"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="增值服务价格" prop="price">
          <el-input v-model="formData.price" type="number" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="增值服务成本价格" prop="costPrice">
          <el-input v-model="formData.costPrice" type="number" placeholder="请输入" />
        </el-form-item>
        
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showEdit=false">取消</el-button>
          <el-button type="primary" @click="editSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 删除确认框 -->
    <el-dialog title="提示" :visible.sync="deleteVisible" width="30%">
      <span>确定删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteComfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {insertAdd,deleteAdd,updateAdd,selectAllAdd} from "@/api/quotationSystem";
export default {
  data() {
    return {
      list: [],
      currentPage: 1,
      total: 0,
      pageSize: 10,
      formData: {},
      showEdit: false,
      deleteVisible: false,
      editIndex:null,
      deleteRow:null
    };
  },
  async mounted() {
    // console.log(service)
    // swlService.select();
    this.getList()
  },
  methods: {
    async getList(){
      const res = await selectAllAdd()
      // this.$set(this.list,res)
      
      this.list = res;
      this.list = this.list.filter(item => item)
      this.total = res.length
    },
    handleEdit(row,index) {
      console.log(index)
      this.formData = row;
      this.showEdit = true;
      this.editIndex=index
    },
    handleDelete(row) {
      this.deleteRow = row
      this.deleteVisible = true
    },
    async deleteComfirm(){
      const res = await deleteAdd({
        id: this.deleteRow.id
      })
      this.$message.success(res)
      this.deleteVisible=false
      this.getList();
    },
    async editSubmit() {
      let res = ''
      if (this.formData.id) {
        //编辑
        res=await updateAdd(this.formData)
      } else {
        //新增
        res=await insertAdd(this.formData)
      }
      this.getList()
      this.formData={}
      this.$message.success(res)
      this.showEdit=false
    }
  }
};
</script>